package dang.action.main;

import java.util.List;

import dang.action.DangAction;
import dang.common.Constant;
import dang.dao.DaoFactory;
import dang.entity.Category;
import dang.entity.Product;
import dang.exception.DangException;

public class BookListAction extends DangAction{
	private Integer twoId;//接收二级栏目ID
	private String twoIdName;
	private String threeIdName;
	private Integer threeId;//接收三级栏目ID
	private List<Category> cats;//返回左侧栏目列表
	private List<Product> books;//返回中间产品信息
	private int totalpnum=0;//返回全部产品数量
	
	//排序方法
	private String panxu="b.dang_price";
	
	//分布功能
	private int size=5;
	private int page=1;//page>=1,page<=totalPages
	private int totalPages=1;
	
	
	public String execute() throws DangException{
		//根据二级栏目ID获取子项
		cats = DaoFactory.getCategoryDao().findByParentId(twoId);
		//统计全部数量
		for(Category c:cats){
			totalpnum+=c.getPnum();
		}
		//获取产品总数
		int totalRecord = 0;
		if(threeId == null){
			totalRecord = totalpnum;
		}else{
			for(Category c:cats){
				if(threeId.equals(c.getId() )){
					totalRecord = c.getPnum();
					break;
				}
			}
		}
		//根据总记录数计算总页数
		if(totalRecord == 0){
			totalPages = 1;
		}else if(totalRecord % size == 0){
			totalPages = totalRecord/size;
		}else{
			totalPages = totalRecord/size+1;
		}
		//根据ID加载产品信息
		Integer catId = threeId==null?twoId:threeId;
		if(page>totalPages){//page>=1,page<=totalPages
			page=totalPages;
		}else if(page<1){
			page=1;
		}
		int begin = (page-1)*size;
		books = DaoFactory.getProductDao(Constant.TYPE_BOOK).findByCatId(catId,begin,size,panxu);
		//get twoIdName
		twoIdName=DaoFactory.getCategoryDao().findById(twoId);
		return "success";
		
		
	}
	
	public List<Category> getCats() {
		return cats;
	}
	public void setCats(List<Category> cats) {
		this.cats = cats;
	}
	public Integer getTwoId() {
		return twoId;
	}
	public void setTwoId(Integer twoId) {
		this.twoId = twoId;
	}
	public Integer getThreeId() {
		return threeId;
	}
	public void setThreeId(Integer threeId) {
		this.threeId = threeId;
	}
	public int getTotalpnum() {
		return totalpnum;
	}
	public void setTotalpnum(int totalpnum) {
		this.totalpnum = totalpnum;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public List<Product> getBooks() {
		return books;
	}

	public void setBooks(List<Product> books) {
		this.books = books;
	}

	public String getTwoIdName() {
		
		
		return twoIdName;
	}

	public void setTwoIdName(String twoIdName) {
		this.twoIdName = twoIdName;
	}

	public String getThreeIdName() {
		for (Category c : cats) {
			if(c.getId()==threeId){
				threeIdName=c.getName();
				break;
			}
		}
		return threeIdName;
	}

	public void setThreeIdName(String threeIdName) {
		this.threeIdName = threeIdName;
	}

	public String getPanxu() {
		return panxu;
	}

	public void setPanxu(String panxu) {
		this.panxu = panxu;
	}
}
